package com.yanqu.road.dao.impl.activity.worldcup;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.climbtower.ClimbTowerTopData;
import com.yanqu.road.entity.activity.worldcup.UserWorldCupBallerData;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserWorldCupBallerDataDaoImpl extends TempDataObjectDao<UserWorldCupBallerData> {

    public static String getUserWorldCupBallerDataSql = "select * from `t_u_worldcup_baller` where `activityId`=? and `userId`=? and entryTime=0 and recycleTime=0";
    public List<UserWorldCupBallerData> getUserWorldCupBallerData(int activityId, long userId) {
        List<UserWorldCupBallerData> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserWorldCupBallerDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserWorldCupBallerData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateUserWorldCupBallerDataRecycleTimeSql = "update `t_u_worldcup_baller` set recycleTime=? where activityId=? and userId=? and ballerId=?";
    public boolean updateDataRecycleTime(List<UserWorldCupBallerData> ballers) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        for (UserWorldCupBallerData data : ballers) {
            data.setBeginWriteTime();
            int i = 1;
            Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
            para.put(i++, new DbParameter(Types.BIGINT, data.getRecycleTime()));

            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getBallerId()));

            getDb().executeNoneQuery(updateUserWorldCupBallerDataRecycleTimeSql, para);
        }

        return true;
    }

    public static String insertUserWorldCupBallerDataSql = "insert into `t_u_worldcup_baller`(activityId,userId,ballerId,type,level,quality,ability,entryTime,recruitTime,recycleTime) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserWorldCupBallerData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBallerId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getQuality()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAbility()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEntryTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRecruitTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRecycleTime()));
        if (getDb().executeNoneQuery(insertUserWorldCupBallerDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserWorldCupBallerDataSql = "update `t_u_worldcup_baller` set entryTime=?,recycleTime=? where activityId=? and userId=? and ballerId=?";

    @Override
    protected boolean updateData(UserWorldCupBallerData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getEntryTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getRecycleTime()));

        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBallerId()));
        if (getDb().executeNoneQuery(updateUserWorldCupBallerDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserWorldCupBallerData parseData(ResultSet rs) throws SQLException {
        UserWorldCupBallerData data = new UserWorldCupBallerData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setBallerId(rs.getInt("ballerId"));
        data.setType(rs.getInt("type"));
        data.setLevel(rs.getInt("level"));
        data.setQuality(rs.getInt("quality"));
        data.setAbility(rs.getInt("ability"));
        data.setEntryTime(rs.getLong("entryTime"));
        data.setRecruitTime(rs.getLong("recruitTime"));
        data.setRecycleTime(rs.getLong("recycleTime"));
        return data;
    }
}
